// pages/goodControl/syncGoods/syncGoods.js
import {
  synchro,
  getCity,
  synchroEdit,
  synchroDetail,
  productInfo,
  defaultadd,
  productDetailNew
} from '../../../api.js'
let app = getApp();
Page({
  data: {
    add: '',
    region: ['省', '市', '区'],
    customItem: '请选择',
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 200,
    imgUrls_length: 0,
    imgUrls_num: 1,
    syncyosis: '波西米亚风格，865颗水钻人工镶嵌，尽享罗马大街风范..',
    showTextArea: false, //默认不显示描述文本框
    showTextAreaColor:false,
    sysnosisFocus: false, //默认商品描述不聚焦
    sysnosisFocusColor:false,
    imgUrls: [],
    syncInfo: {
      id: 121,
      price: '', //销售价
      synopsis: '', //商品描述
      is_autodeliver: 0, //是否自发货
      name: '', //发货人
      phone: '', //联系方式
      province: '', //省（编码）
      city: '', //市（编码）
      district: '', //区 （编码）
      address: '', //详细地址
      syncInfo:''
    },
    types: 0, //types=1 编辑全网 types=0 同步入库
    brand_title: '', //品牌名
    brand_id:'',
    brand_other:'',
    systematics_other:'',
    systematics_title: '', //系列名
    full_name: '', //产品名称
    condition: '', //商品成色
    peer_price: '', //同行价
    merShortName: '', // 发货人 待发货
    phone: '', //联系方式 待发货 
    province: [],
    city: [],
    area: [],
    index: 0,
    index2: 0,
    index3: 0,
    showProvince: false,
    showCity: false,
    showArea: false,
    priceFocus: false,
    windowHeight: '',
    rectTop: '',
    EitiLis:'',
  },
  onLoad: function(options) {
    console.log('options', options)
    var _this = this;
    var id = Number(options.id);
    getCity({
      token: app.globalData.token
    }).then(res => {
      console.log('省市区', res)
      _this.setData({
        province: res.data.data,
      })
    })
    if (options.type == '0') { //同步入库
      console.log('00000')
      productDetailNew({
        token: app.globalData.token,
        id
      }).then(res => {
        if (res.data.code == 666) {
          console.log(res.data.data)
          var data = res.data.data;
          if (res.data.data.business_country != '中国'){
            wx.showModal({
              title: '提示',
              content: '订单支付金额低于5000元时不含关税，若商品售价超过5000元被清关抽检，需由最终买家补缴关税',
              showCancel:false,
              success(res){
                if(res.confirm){
                  wx.showModal({
                    title: '提示',
                    content: '您同步的商品为' + data.business_country +'直邮商品，发货方式仅支持代发货',
                    showCancel: false,
                    success(res) {
                    }
                  })
                }
              }
            })
          }
          _this.setData({
            'syncInfo.id': id,
            types: 0,
            isSysnc: true,
            imgUrls: data.image,
            imgUrls_length: data.image.length,
            full_name: data.product_name,
            condition: data.finess_value,
            peer_price: data.peer_price,
            merShortName: data.business_name ? data.business_name : ' ',
            phone: data.business_phone ? data.business_phone : '',
            'syncInfo.synopsis': data.synopsis ? data.synopsis : '暂无信息',
            'syncInfo.fineness_description': data.fineness_description ? data.fineness_description : '暂无信息',
            'syncInfo.is_autodeliver': data.is_autodeliver ? data.is_autodeliver : 0,
            EitiLis: data
          })
        } else if (res.data.code == 111111) {
          wx.showToast({
            title: '登录失效，请重新登录',
            icon: 'none',
          })
          setTimeout(function() {
            wx.reLaunch({
              url: '/pages/user/login/login',
            })
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }

      })
    }
    if (options.type == '1' ) { //编辑全网
      productDetailNew({
        token: app.globalData.token,
        id
      }).then(res => {
        if (res.data.code == 666) {
          var data = res.data.data;
          console.log(data)
          _this.setData({
            'syncInfo.id': id,
            types: 1,
            isSysnc: false,
            imgUrls: data.image,
            imgUrls_length: data.image.length,
            brand_title: data.brand_title,
            brand_id: data.brand_id,
            systematics_other: data.systematics_other,
            brand_other: data.brand_other,
            systematics_title: data.systematics_title,
            full_name: data.product_name,
            condition: data.finess_value,
            peer_price: data.peer_price,
            merShortName: data.business_name ? data.business_name: ' ',
            phone: data.business_phone ? data.business_phone : data.merLegalPhone,
            'syncInfo.id': options.id,
            'syncInfo.price': data.price,
            'syncInfo.synopsis': data.synopsis ? data.synopsis : '暂无信息',
            'syncInfo.is_autodeliver': data.is_autodeliver,
            'syncInfo.name': data.is_autodeliver == 1 ? data.delivery_id.name : '',
            'syncInfo.phone': data.is_autodeliver == 1 ? data.delivery_id.phone : '',
            'syncInfo.province': data.is_autodeliver == 1 ? data.delivery_id.province : '',
            'syncInfo.city': data.is_autodeliver == 1 ? data.delivery_id.city : '',
            'syncInfo.district': data.is_autodeliver == 1 ? data.delivery_id.district : '',
            'syncInfo.address': data.is_autodeliver == 1 ? data.delivery_id.address : '',
            'syncInfo.fineness_description': data.fineness_description ? data.fineness_description : '暂无信息',
            // 'syncInfo.updelivery_id': data.delivery_id.length != 0 ? data.delivery_id.id : '',
            EitiLis:data
          })
        } else if (res.data.code == 111111) {
          wx.showToast({
            title: '登录失效，请重新登录',
            icon: 'none',
          })
          setTimeout(function() {
            wx.reLaunch({
              url: '/pages/user/login/login',
            })
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }

      })
    }
    wx.getSystemInfo({
      success: function(res) {
        console.log(res.windowHeight)
        _this.setData({
          windowHeight: res.windowHeight
        })
      },
    })
    wx.createSelectorQuery().select('#the-id').boundingClientRect(function(rect) {
      console.log(rect.top)
      _this.setData({
        rectTop: rect.top
      })
    }).exec()
  },
  onUnload: function() {
    let controlType = (this.data.types == 1) ? 'goodControl' : 'allGoodControl'
    wx.setStorageSync('controlType', controlType)
  },
  showToast() {
    this.setData({
      showTradePrice: true
    })
  },
  // 显示隐藏商品描述文本框
  isShow() {
    this.setData({
      showTextArea: true,
      sysnosisFocus: true
    })
  },
  isShowColor() {
    this.setData({
      showTextAreaColor: true,
      sysnosisFocusColor: true
    })
  },
  closed() {
    this.setData({
      showTradePrice: false
    })
  },
  bindchange: function(e) {
    var num = e.detail.current;
    var that = this;
    that.setData({
      imgUrls_num: num + 1
    })
  },
  chooseProvince(e) {
    let label = e.currentTarget.dataset.address;
    console.log(label)
    this.setData({
      [label]: true
    })
    console.log('e', e)
  },
  //商户经营省
  bindProvince: function(e) {

    this.setData({
      index: e.detail.value,
    })
    if (this.data.city.length > 0) {
      this.setData({
        city: [],
        area: [],
        showCity: false,
        showArea: false
      })
    }
    let city = this.data.province[this.data.index].children;
    let merProvince = this.data.province[this.data.index].value;
    this.setData({
      city,
      'syncInfo.province': merProvince
    })
  },
  //商户经营市
  bindCity: function(e) {
    this.setData({
      index2: e.detail.value,
    })
    if (this.data.area.length > 0) {
      this.setData({
        area: [],
        showArea: false
      })
    }
    let area = this.data.city[this.data.index2].children;
    let merCity = this.data.city[this.data.index2].value;
    this.setData({
      area,
      'syncInfo.city': merCity
    })
  },
  //商户经营区
  bindArea: function(e) {
    this.setData({
      index3: e.detail.value,
    })
    let merDistrict = this.data.area[this.data.index3].value;
    this.setData({
      'syncInfo.district': merDistrict
    })
  },
  bindName(e) {
    var key = `${e.currentTarget.dataset.name}`;
    this.setData({
      [key]: e.detail.value
    })
  },
  bindNameColor(e) {
    this.setData({
      'syncInfo.fineness_description': e.detail.value
    })
  },
  deliverType(e) {
    var that = this
    this.setData({
      'syncInfo.is_autodeliver': e.detail.value
    })
    if (e.detail.value == 1) {
      defaultadd({
        token: app.globalData.token
      }).then(res => {
        if (res.data.code == 666) {
          if (res.data.data.length != 0) {
            that.data.syncInfo.name = res.data.data[0].name
            that.data.syncInfo.phone = res.data.data[0].phone
            that.data.syncInfo.province = res.data.data[0].province
            that.data.syncInfo.city = res.data.data[0].city
            that.data.syncInfo.district = res.data.data[0].district
            that.data.syncInfo.address = res.data.data[0].address
            that.setData({
              syncInfo: that.data.syncInfo
            })
          }
        } else if (res.data.code == 111111) {
          wx.showToast({
            title: '登录失效，请重新登录',
            icon: 'none',
          })
          setTimeout(function() {
            wx.reLaunch({
              url: '/pages/user/login/login',
            })
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            mask: true
          })
        }
      })
    }
  },
  cancel(e) {
    if (this.data.types == 1) { //编辑(商品库存)
      wx.setStorageSync('controlType', 'goodControl');
    }
    wx.switchTab({
      url: '/pages/goodControl/goodControl',
    })
  },
  confirm(e) {
    if (this.data.syncInfo.price == '') {
      wx.showToast({
        title: '请输入销售价',
        icon: 'none',
        mask: true,
      })
      this.setData({
        priceFocus: true
      })
      return
    }
    if ((this.data.syncInfo.price < this.data.peer_price)) {
      wx.showToast({
        title: '请设置销售价不小于同行价',
        icon: 'none',
        mask: true,
      })
      this.setData({
        priceFocus: true
      })
      return
    }
    // if (this.data.syncInfo.synopsis == '') {
    //   wx.showToast({
    //     title: '请输入商品描述',
    //     icon: 'none',
    //     mask: true,
    //   })
    //   return
    // }
    var _this = this;
    var syncInfo = _this.data.syncInfo;
    syncInfo.token = app.globalData.token;
    if (_this.data.types == 1) { //编辑(商品库存)
      synchroEdit(syncInfo).then(res => {
        if (res.data.code == 666) {
          wx.setStorageSync('controlType', 'goodControl');
          wx.switchTab({
            url: '/pages/goodControl/goodControl',
          })
        } else if (res.data.code == 111111) {
          wx.showToast({
            title: '登录失效，请重新登录',
            icon: 'none',
          })
          setTimeout(function() {
            wx.reLaunch({
              url: '/pages/user/login/login',
            })
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            mask: true
          })
        }
      })


    }
    if (_this.data.types == 0) {
      synchro(syncInfo).then(res => {
        if (res.data.code == 666) {
          wx.switchTab({
            url: '/pages/fullStock/fullStock',
          })
        } else if (res.data.code == 111111) {
          wx.showToast({
            title: '登录失效，请重新登录',
            icon: 'none',
          })
          setTimeout(function() {
            wx.reLaunch({
              url: '/pages/user/login/login',
            })
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            mask: true,
          })
        }
      })
    }
  },

  bindRegionChange: function(e) {
    var val = e.detail.value;
    this.setData({
      region: e.detail.value,
      'syncInfo.province': e.detail.value[0], //省（编码）
      'syncInfo.city': e.detail.value[1], //市（编码）
      'syncInfo.district': e.detail.value[2], //区 （编码）
    })
  },
  onPageScroll(e) {
    var windowHeight = this.data.windowHeight
    var rectTop = this.data.rectTop
    var _this = this
    if (e.scrollTop > rectTop - windowHeight + 100) {
      _this.setData({
        showTextArea: false,
        showTextAreaColor:false
      })
    }
  },


})